<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style type="text/css">
        .class-title {
            margin-top: 10px;
            margin-left: 10px;
            margin-right: 10px;
            font-size: 20px;
            font-weight: bold;
            color: #00BFFF;
        }

        .class-hr {
            margin-top: 5px;
            height: 1px;
            border: none;
            border-top: 1px solid #DCDCDC;
        }

        .class-seqno {
            margin-top: 3px;
            margin-left: 10px;
            margin-right: 25px;
            font-size: 10px;
            color: #808080;
            font-weight: bold;
            text-align: right;
        }

        .class-table {
            width: 100%;
            text-align: left;
            color: black;
            font-size: 13px;
            margin-left: 2%;
            width: 100%;
            table-layout: fixed;
        }

        .class-td {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .lw {
            background-color: #DCE9F2;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
            font-weight: bold;
        }

        .lw1 {
            background-color: #f0f5f5;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
        }

        .table {
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
        }

        .table td {
            height: 40px;
            text-align: left;
            border: 0px solid #CBD6DE;
            padding: 0;
            margin: 0;
        }

        .aui-searchbar-input {
            width: 95% !important;
        }
    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            <!-- <span>单号:</span> -->
            <span id="titleVal"></span>
        </div>
    </header>
    <div class="aui-content aui-margin-b-15">
        <ul class="ui-selectee">
            <li class='sortable ui-selectee ui-sortable'>
                <ul class='aui-list aui-media-list ui-selectee'>
                    <li class='aui-list-item ui-selectee'>
                        <div class='aui-media-list-item-inner ui-selectee'>
                            <div class='aui-list-item-inner ui-selectee'>
                                <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                                    <div class='aui-list-item-label ui-selectee list-tr-title'>发货日期</div>
                                    <div class='aui-list-item-inner ui-selectee' id="fhrq"></div>
                                </div>
                                <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                                    <div class='aui-list-item-label ui-selectee list-tr-title'>客户编码</div>
                                    <div class='aui-list-item-inner ui-selectee' id="gysbm"></div>
                                </div>
                                <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                                    <div class='aui-list-item-label ui-selectee list-tr-title'>客户名称</div>
                                    <div class='aui-list-item-inner ui-selectee' id="gysmc"></div>
                                </div>
                                <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                                    <div class='aui-list-item-label ui-selectee list-tr-title'>物料编码</div>
                                    <div class='aui-list-item-inner ui-selectee' id="wlbm"></div>
                                </div>
                                <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                                    <div class='aui-list-item-label ui-selectee list-tr-title'>物料名称</div>
                                    <div class='aui-list-item-inner ui-selectee' id="wlmc"></div>
                                </div>
                                <div class='aui-list-item-text ui-selectee' style='height: 30px;'>
                                    <div class='aui-list-item-label ui-selectee list-tr-title' style="width:200%">计划发货数量
                                    </div>
                                    <div class='aui-list-item-inner ui-selectee' id="jhfhsl" style='margin-left: 20px;'>
                                    </div>
                                    <div class='aui-list-item-inner ui-selectee'
                                        style="text-align: right;color: #494e4e; margin-right: 30px; width:150%">绑定数量
                                    </div>
                                    <!-- <div class='aui-list-item-right ui-selectee' id="sjfhsl" style='margin-left: 30px;'> -->
                                    <div class='aui-list-item-inner ui-selectee' id="sjfhsl"
                                        style='margin-left: -20px;'>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </li>
                </ul>
            </li>
        </ul>

        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">仓库</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 仓库" id="workCenterCode" value=""
                            onclick="getIdSelector();" readonly="readonly" />
                    </div>
                    <!-- <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenter()"></i> -->
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 库位" id="workCellCode" value="" onclick="getIdSelector();"
                            readonly="readonly" />
                    </div>
                    <!-- <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell()"></i> -->
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描物料条码" id="codeBar" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
        </ul>

        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="delDetail()">清空明细</div>

        <div style="width:100%;margin-top:2%;"></div>
        <!-- <div class="aui-btn aui-btn-info aui-btn-block" style="background-color: #c8e1ec !important;">
            <div class="aui-searchbar" id="search" style="position: relative;background-color:#c8e1ec">
                <div class="aui-searchbar-input aui-border-radius">
                    <i class="aui-iconfont aui-icon-search"></i>
                    <div class="aui-searchbar-input">
                        <form>
                            <input type="text" placeholder="请扫描物料条码" id="codeBar" value="" onclick="getIdSelector();" />
                        </form>
                    </div>
                </div>
                <div class="aui-btn aui-btn-primary" style="margin-right: 0.5rem;background-color:#ef0909;" onclick="delDetail()">清空明细</div>
            </div>
        </div> -->
        <div id="divhtml" style="display: block;width:100%;height: 490px;overflow-y: scroll;margin-bottom:50px;">
            <ul class="ui-selectee" id="tbodyHtml">

            </ul>
        </div>

    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script>
    //光标所在id
    var idSelector = "";
    var workCenterGid = ""; //仓库GID
    var workCenterCode = ""; //仓库编码
    var workCenterName = ""; //仓库名称
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称

    var lotCode = "";
    var qty = "";
    var boxnum = "";
    var tmId = "";

    apiready = function () {

        $api.html($api.byId("titleVal"), api.pageParam.code);
        $api.html($api.byId("fhrq"), api.pageParam.invoicedate);
        $api.html($api.byId("gysbm"), api.pageParam.customerSoldCode);
        $api.html($api.byId("gysmc"), api.pageParam.customerSoldName);
        $api.html($api.byId("wlbm"), api.pageParam.mrlCode);
        $api.html($api.byId("wlmc"), api.pageParam.mrlName);
        $api.html($api.byId("jhfhsl"), api.pageParam.planQty);
        $api.html($api.byId("sjfhsl"), api.pageParam.actualQty);


        idSelector = "codeBar";
        $("#codeBar").focus();

        //获取发货单已经绑定的明细
        getDetailList();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //获取发货单已经绑定的明细
    function getDetailList() {

        //发货单号
        var name = api.pageParam.code;
        if (name != null && name != undefined && name != "") {
            name = name.trim();
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoiceinfoDetailList";
        var data = {
            values: {
                invoiceinfocode: name,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    var actualQty = 0;
                    for (var i = 0; i < list.length; i++) {
                        actualQty = actualQty + list[i].qty;
                        html += "<li class='sortable ui-selectee ui-sortable'  onclick='delDetailSingle(\"" + list[i].sysid + "\",\"" + list[i].packageno + "\")'>";
                        html += "<ul class='aui-list aui-media-list ui-selectee'>";
                        html += "<li class='aui-list-item  ui-selectee' style='color: red !important;background-color: #addbef !important;'>";
                        html += "<div class='aui-media-list-item-inner ui-selectee'>";
                        html += "<div class='aui-list-item-inner ui-selectee'>";
                        // html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        // html += "<div class='aui-list-item-label ui-selectee list-tr-title'>托号</div>";
                        // html += "<div class='aui-list-item-inner ui-selectee'>" + (list[i].traycode == null ? "" : list[i].traycode) + "</div>";
                        // html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>箱号</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].packageno + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>批次号</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].batchno + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>数量</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].qty + "</div>";
                        html += "</div>";

                        // html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        // html += "<div class='aui-list-item-label ui-selectee list-tr-title' style='width: 45%;'>实际发货数量</div>";
                        // html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 0.1rem;'>" + list[i].actualQty + "</div>";
                        // html += "<div class='aui-list-item-right aui-btn ui-selectee' "
                        //  +"onclick='goPageDetail(\""+list[i].sysid+"\",\""+list[i].code+"\",\""+list[i].invoicedate+"\",\""+list[i].customerSoldCode+"\",\""+list[i].customerSoldName+"\",\""+list[i].mrlCode+"\",\""+list[i].mrlName+"\",\""+list[i].planQty+"\");'>发货</div>";
                        // html += "</div>";
                        html += "</div>";
                        html += "</div>";
                        html += "</li>";
                        html += "</ul>";
                        html += "</li>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);
                    $api.html($api.byId("sjfhsl"), actualQty);
                    //失败
                } else if (myobj.code == "1") {
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：仓库
            if (idSelector == 'workCenterCode') {

                $("#workCenterCode").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector);

                //2:库位
            } else if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

                //扫描条码
            } else if (idSelector == 'codeBar') {

                $("#codeBar").val(ret);
                saveData();
            }
        });
    }

    //1：仓库扫描校验
    function getWorkCenter(code, inputId) {
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCenter";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCenterCode: code,
                appVersion: api.appVersion,
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCenterGid = list[0].workCenterGid;
                    workCenterCode = list[0].workCenterCode;
                    workCenterName = list[0].workCenterName;
                    document.getElementById(inputId).value = list[0].workCenterCode + ":" + list[0].workCenterName;
                    $("#workCellCode").focus();
                    idSelector = "workCellCode";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCenterCode").focus();
                    $("#workCenterCode").select();
                }
            }
        });
    }

    //2：选中仓库
    function goSelectWorkCenter() {
        api.openWin({
            name: 'uwmSelectWorkCenter',
            url: 'uwmSelectWorkCenter.html',
            pageParam: {
                flag: "in",
                name: "wmInvoiceInfoDetail",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }

    //仓库选择后回调函数
    function setWorkCenter(id, code, name) {
        workCenterGid = id;
        workCenterCode = code;
        workCenterName = name;
        document.getElementById('workCenterCode').value = code + ":" + name;
        $("#workCellCode").focus();
        idSelector = "workCellCode";
    }

    //3；PDA自带扫描回车事件
    $("#workCenterCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCenterCode").val(sm);
            // console.log("扫描回车事件：" + sm);
            //校验改箱码是否存在
            getWorkCenter(sm, "workCenterCode");
        }
    });

    //5:库位扫描校验
    function getWorkCell(code, inputId) {
        //扫库位前先扫仓库
        if (inputId == "workCellCode") {
            if (workCenterGid == "" || workCenterGid == null || workCenterGid == undefined) {
                api.toast({
                    msg: "请先选择/扫描 仓库",
                    duration: 3000
                });
                $("#workCellCode").val("");
                workCellCode = "";
                workCellGid = "";
                workCellName = "";
                return;
            }
        }
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: workCenterGid, //仓库GID
                appVersion: api.appVersion,
                workCellCode: code, //库位编码
                relWorkCellGid: "", //目标库位
                tm: "",
                workCellFlag: "", //原库位目标库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#codeBar").focus();
                    idSelector = "codeBar";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }
            }
        });
    }

    //6：选择库位
    function goSelectWorkCell() {
        var tm = $("#codeBar").val();
        //仓库非空校验
        if (workCenterGid == "" || workCenterGid == null || workCenterGid == undefined) {
            api.toast({
                msg: "请先选择/扫描 仓库",
                duration: 3000
            });
            return;
        }

        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "in_rk",
                name: "wmInvoiceInfoDetail",
                tm: tm,
                workCenterGid: workCenterGid,
                relWorkCellGid: "",
                workCellFlag: ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name) {
        workCellGid = id;
        workCellCode = code;
        workCellName = name;
        document.getElementById('workCellCode').value = code + ":" + name;
        $("#codeBar").focus();
        idSelector = "codeBar";
    }

    //7；PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCell(sm, "workCellCode");
        }
    });


    //3；PDA自带扫描回车事件
    $("#codeBar").on('keypress', function (e) { //#keyword为input文本框

        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            // $("#codeBar").val(sm);
            // saveData();
            if (sm != null && sm != "" && sm != undefined) {
                console.log(sm)
                $("#codeBar").blur();
                //调用条码解析+保存事件
                saveData(sm);
            }
        }
    });

    function saveData(tm) {
        //解析出条码中的 物料、批次 进行匹配明细
        //YDGYS-A,2810010-BQ01,2,2021031301,e7e26a1af372470987d7c5acb0040547,,,,,B-20210807-1918
        //供应商编码，物料编码，数量，批次号，不知道，，，，，箱号
        //var codeBar = $("#codeBar").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                //tm: codeBar, //条码
                tm: tm,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;

                    var mrlCode = map['mrlCode'];
                    lotCode = map['lotCode'];
                    qty = map['qty'];
                    boxnum = map['boxnum'];
                    tmId = map['tmID'];

                    if (mrlCode != api.pageParam.mrlCode) {
                        api.toast({
                            msg: '物料不匹配',
                            duration: 3000
                        });
                        $("#codeBar").val("");
                        $("#codeBar").select();
                        idSelector = "codeBar";
                        return;
                    }
                    getWorkCellBytM(tm, boxnum)
                    //自动保存
                    // saveAuto();
                } else if (myobj.code == '1') {

                    $("#codeBar").val("");
                    $("#codeBar").select();
                    idSelector = "codeBar";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#codeBar").val("");
                $("#codeBar").select();
                idSelector = "codeBar";
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //根据箱码获取来源库位
    function getWorkCellBytM(tm, boxnum) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCellBytM";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                tm: tm,
                boxnum: boxnum,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log(JSON.stringify(myobj));
                if (myobj.type == "0") {

                    if (myobj.id != "" && myobj.id != null && myobj.id != undefined) {
                        workCellGid = myobj.id;
                    }

                    workCellCode = myobj.code;

                    $('#workCellCode').val(myobj.code + ":" + myobj.name);
                    getWorkCenterByCell(workCellGid)

                    //失败
                } else if (myobj.type == "1") {
                    alert(myobj.message);
                    $("#codeBar").val("");
                    $("#codeBar").select();
                    idSelector = "codeBar";
                }
                //返回消息
                // if (myobj.id != "" && myobj.id != null && myobj.id != undefined) {
                //     workCellGid = myobj.id;
                // }

                // workCellCode = myobj.code;

                // if (myobj.type == '1') {
                //     alert(myobj.message);
                //     $("#codeBar").val("");
                //     $("#codeBar").select();
                //     idSelector = "codeBar";

                // } else {
                //     $('#workCellCode').val(myobj.code + ":" + myobj.name);
                //     getWorkCenterByCell(workCellGid)
                // }

            }
        });
    }
    //根据获取来源库位,获取仓库
    function getWorkCenterByCell(workCellGid) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getworkWay";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                workCellGid: workCellGid,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == "0") {

                    //返回消息
                workCenterCode = myobj.centerCode;
                if (myobj.type == '1') {
                    alert(myobj.message);
                    $("#codeBar").val("");
                    $("#codeBar").select();
                    idSelector = "codeBar";
                } else {
                    $('#workCenterCode').val(myobj.centerCode + ":" + myobj.centerName);
                    //自动保存
                    saveAuto();
                }
                    //失败
                } else if (myobj.code == "1") {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
                //返回消息
                // workCenterCode = myobj.centerCode;
                // if (myobj.type == '1') {
                //     alert(myobj.message);
                //     $("#codeBar").val("");
                //     $("#codeBar").select();
                //     idSelector = "codeBar";
                // } else {
                //     $('#workCenterCode').val(myobj.centerCode + ":" + myobj.centerName);
                //     //自动保存
                //     saveAuto();
                // }

            }
        });
    }

    //自动保存
    function saveAuto() {
        var codeBar = $("#codeBar").val();

        //仓库非空校验
        if (workCenterCode == "" || workCenterCode == null || workCenterCode == undefined) {
            return;
        }

        //库位非空校验
        if (workCellCode == "" || workCellCode == null || workCellCode == undefined) {
            return;
        }

        //仓库非空校验
        if (codeBar == "" || codeBar == null || codeBar == undefined) {
            return;
        }

        save();
    }

    function save() {

        var codeBar = $("#codeBar").val();

        //仓库非空校验
        if (workCenterCode == "" || workCenterCode == null || workCenterCode == undefined) {
            api.toast({
                msg: "请选择/扫描 仓库",
                duration: 1500
            });
            return;
        }

        //库位非空校验
        if (workCellCode == "" || workCellCode == null || workCellCode == undefined) {
            api.toast({
                msg: "请选择/扫描 库位",
                duration: 1500
            });
            return;
        }

        //仓库非空校验
        if (codeBar == "" || codeBar == null || codeBar == undefined) {
            api.toast({
                msg: "请扫描 条码",
                duration: 1500
            });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoiceinfoDetailSave";
        var data = {
            un: un,
            invoiceinfoid: api.pageParam.sysid,
            invoiceinfocode: api.pageParam.code,
            mrlCode: api.pageParam.mrlCode,
            mrlName: api.pageParam.mrlName,
            appVersion: api.appVersion,
            packageno: boxnum,
            batchno: lotCode,
            qty: qty,
            tmId: tmId,
            workCenterCode: workCenterCode,
            workCellCode: workCellCode,
            tm: codeBar
        };
        $.ajax({
            type: 'post',
            async: false,
            url: url,
            data: data,
            dataType: 'json',
            success: function (ret) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");
                    //成功
                    if (myobj.code == '0') {
                        api.hideProgress();
                        api.toast({
                            msg: "保存成功",
                            duration: 3000
                        });

                        //清空表单
                        $("#codeBar").val("");
                        $("#codeBar").focus();
                        $("#codeBar").select();
                        idSelector = "codeBar";

                        workCenterCode = "";
                        $('#workCenterCode').val("");
                        $('#workCellCode').val("");
                        getDetailList();

                    } else if (myobj.code == '1') {
                        $("#codeBar").val("");
                        workCenterCode = "";
                        $('#workCenterCode').val("");
                        $('#workCellCode').val("");
                        api.hideProgress();
                        $("#codeBar").select();
                        idSelector = "codeBar";
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                    }
                } else {
                    $("#codeBar").val("");
                    $("#codeBar").select();
                    idSelector = "codeBar";
                    api.hideProgress();
                    alert("连接超时，请耐心等待");
                }
            }
        });
    }


    //删除所有明细
    function delDetail() {
        api.confirm({
            title: '提示',
            msg: '您确定要删除已经扫描的所有明细吗？',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            if (ret.buttonIndex == 1) {
                var un = localStorage.getItem('un');
                var data = {
                    values: {
                        un: un,
                        invoiceinfoid: api.pageParam.sysid,
                        invoiceinfocode: api.pageParam.code,
                        flag: "ALL" //ALL 删除所有明细 SINGLE 单个删除
                    }
                };
                delData(data);
            }
        });
    }
    //删除单个明细
    function delDetailSingle(sysid, packageno) {
        api.confirm({
            title: '提示',
            msg: '您确定要删除装箱单号为【' + packageno + '】的明细吗？',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            if (ret.buttonIndex == 1) {
                var un = localStorage.getItem('un');
                var data = {
                    values: {
                        un: un,
                        invoiceinfoid: api.pageParam.sysid,
                        sysid: sysid,
                        appVersion: api.appVersion,
                        flag: "SINGLE" //ALL 删除所有明细 SINGLE 单个删除
                    }
                };
                delData(data);
            }
        });
    }

    function delData(data) {

        api.showProgress({
            title: '操作中...',
        });

        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoiceinfoDetaildelete";
        

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "删除成功",
                        duration: 3000
                    });

                    //清空表单
                    $("#codeBar").val("");
                    getDetailList();

                } else if (myobj.code == '1') {
                    $("#codeBar").val("");
                    api.hideProgress();
                    $("#codeBar").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }
    //返回按钮
    function closeWin() {
        api.closeWin({});

        api.execScript({
            name: "wmInvoiceInfo",
            script: 'refreshData()'
        });
    }
</script>

</html>